package com.report.utils;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ZipUtil;
import com.report.enums.FileEnum;
import com.report.file.PdfFile;
import com.zmh.framework.exception.ResultException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author summer
 * @date 2024-06-21 21:25
 */
@Configuration
public class ZipUtils {



    @Value("${outFile}")
    private String outFile;

    public List<PdfFile> unzip(MultipartFile[] files) {
            List<PdfFile> pdfFileList=ListUtil.list(true);
            for (MultipartFile file : files) {
                PdfFile pdfFile = new PdfFile();
                List<PdfFile> lists= ListUtil.list(true);
                try {
                    ZipUtil.unzip(file.getInputStream(), FileUtil.file(outFile),  Charset.forName("GBK"));
                    List<File> fileList = FileUtil.loopFiles(outFile);
                    fileList.forEach(file1 -> {
                        if(FileEnum.PDF.name().equals(FileUtil.extName(file1).toUpperCase())){
                            pdfFile.setFile(file1);
                        }else{
                            PdfFile image = new PdfFile();
                            image.setFile(file1);
                            lists.add(image);
                        }
                    });
                    pdfFile.setFileList(lists);
                    pdfFileList.add(pdfFile);
                } catch (IOException e) {
                    throw new ResultException("文件解压失败");
                }
            }
           return pdfFileList;
    }

    public static void main(String[] args) {
        String input = "aaa123456";
        Pattern pattern = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
        Matcher matcher = pattern.matcher(input);

        if (matcher.matches()) {
            System.out.println("匹配成功！");
        } else {
            System.out.println("匹配失败！");
        }
    }
}
